/*
 * CAreaList.cpp
 *
 *  Created on: 11 Oct 2012
 *      Author: Mario Kleinsasser
 *		Time:	20:05:19
 */

#include "CAreaList.h"
#include "CArea.h"
#include <iostream>
#include <stdlib.h>
#include <time.h>

using namespace std;

CArea *CAreaList::appendVToArea(CArea *area, CArea *prev) {
	/* recursively add objects vertical to arealist */
	CArea *newarea = new CArea();
	area->setSouth(newarea);
	newarea->setNorth(area);

	/*
	 * at first call the prev area is set to null
	 * because left of the most left field is NULL
	 *
	 * if it is not NULL, a call with a prev area happend
	 * now set the prev - east to current and vice versa
	 */
	if (prev != NULL) {
		prev->setEast(newarea);
		newarea->setWest(prev);
	}

	/*
	 * recursive call to fill up the next field east of the current
	 */
	if (area->getEast() != NULL) {
		/* the newarea of the current call
		 * is needed to build up the double linked connections
		 */
		appendVToArea(area->getEast(), newarea);
	}

	return newarea;
}

/* not needed currently - obsolete!!! */
int CAreaList::appendVToArea(CArea *area) {
	CArea *newarea = new CArea();
	area->setSouth(newarea);
	newarea->setNorth(area);

	if (area->getEast() != NULL) {
		appendVToArea(area->getEast(), newarea);
	}

	return 0;
}

int CAreaList::appendHToArea(CArea *area) {
	/* add objects horizontal to arealist */
	CArea *newarea = new CArea();
	area->setEast(newarea);
	newarea->setWest(area);
	this->last = newarea;

	return 0;
}

void CAreaList::actAll() {
	/* let all areas act */
	/* recursive through area
	 *
	 * get the first element, act the first row and continue
	 * with the next rows
	 */
	CArea *temp = this->first;
	temp = actArea(temp);

	cout << "\n";

	/* next rows until
	 * temp will be NULL if south of the element
	 * is no more element
	 */
	while (temp != NULL) {
		temp = actArea(temp);
		cout << "\n";
	}
}

CArea* CAreaList::actArea(CArea *area) {
	/* area->printMe();
	 * getType is of type CItem but derived to needed field class
	 *
	 * get east trough the list an print each element
	 */

	area->getType()->act();

	if (area->getEast() != NULL) {
		actArea(area->getEast());
	}

	/* return the pointer to the south element
	 * if south is NULL then we are at the bottom of the area
	 */
	return area->getSouth();
}


CAreaList::CAreaList(int M, int N) {
	/* create new Area object */
	CArea *area = new CArea();
	this->first = area; /* set the list header first element*/
	this->last = area; /* set the list header last element*/

	/* Fill the first line with elements */
	for (int j = 1; j < N; j++) {
		appendHToArea(this->last); /* Fill the first row up with correct areas */
	}

	/*
	 *  Now the first row is complete
	 *  Start to fill up the columns
	 *
	 *  Start at "first" element and go "down"
	 *  Do this for all columns
	 *
	 *  Get first row element is needed to do it recursive
	 *  temp pointer for build up of area dont loose the first pointer!!!!
	 */

	CArea *temp = this->first;
	for (int i = 1; i < M; i++) {
		temp = appendVToArea(temp, NULL);
	}

}

CArea* CAreaList::getArea(int M, int N){
	/* go through the list and get the N M element */

	CArea *temp = this->first;

	for(int i = 0; i < N-1; i++ ){
		temp = temp->getEast();
	}

	for(int i = 0; i < M-1; i++){
		temp = temp->getSouth();
	}

	return temp;
}

CAreaList::~CAreaList() {
	// TODO Auto-generated destructor stub
}

